//
//  FriendListViewController.m
//  FriendsList
//
//  Created by chen on 16/4/1.
//  Copyright © 2016年 zhangmc. All rights reserved.
//

#import "FriendListViewController.h"
#import "FriendModel.h"
#import "FriendTableViewCell.h"


@interface FriendListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)NSArray *dataArray;

@property(nonatomic, strong)NSMutableDictionary *friendDict;

@property(nonatomic, strong)NSArray *resultArray;

@property(nonatomic, strong)UITableView *tableView;

@end

@implementation FriendListViewController

//- (instancetype)init
//{
//    if (self = [super init]) {
//        
//        FriendModel *model1 = [FriendModel new];
//        model1.name = @"张三";
//        model1.age = 12;
//        
//        FriendModel *model2 = [FriendModel new];
//        model1.name = @"李四";
//        model1.age = 13;
//        
//        FriendModel *model3 = [FriendModel new];
//        model1.name = @"王五";
//        model1.age = 14;
//        
//        FriendModel *model4 = [FriendModel new];
//        model1.name = @"哈喽";
//        model1.age = 15;
//        
//        FriendModel *model5 = [FriendModel new];
//        model1.name = @"陈奕迅";
//        model1.age = 16;
//        
//        FriendModel *model6 = [FriendModel new];
//        model1.name = @"周杰伦";
//        model1.age = 18;
//        
//        FriendModel *model7 = [FriendModel new];
//        model1.name = @"苍老师";
//        model1.age = 20;
//        
//        self.dataArray = @[model1,model2,model3,model4,model5,model6,model7];
//    }
//    
//    return self;
//}

- (NSArray *)dataArray
{
    
    FriendModel *model1 = [FriendModel new];
    model1.name = @"张三";
    model1.age = 12;
    
    FriendModel *model2 = [FriendModel new];
    model2.name = @"李四";
    model2.age = 13;
    
    FriendModel *model3 = [FriendModel new];
    model3.name = @"王五";
    model3.age = 14;
    
    FriendModel *model4 = [FriendModel new];
    model4.name = @"哈喽";
    model4.age = 15;
    
    FriendModel *model5 = [FriendModel new];
    model5.name = @"陈奕迅";
    model5.age = 16;
    
    FriendModel *model6 = [FriendModel new];
    model6.name = @"周杰伦";
    model6.age = 18;
    
    FriendModel *model7 = [FriendModel new];
    model7.name = @"波老师";
    model7.age = 20;
    
    return  @[model1,model2,model3,model4,model5,model6,model7];
}

//- (UITableView *)tableView
//{
//    if (!_tableView) {
//        
//        self.tableView = [[UITableView alloc] init];
//    }
//    return _tableView;
//}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 20, self.view.frame.size.width, self.view.frame.size.height - 20)];
    
//    self.tableView.frame = self.view.frame;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];
    
    [self getData];
}


- (void)getData
{
    NSMutableArray *modelArr = [NSMutableArray array];
    
    NSMutableSet *enSet = [NSMutableSet set];
    
    self.dataArray = [self dataArray];

    //遍历数据源。取出姓名，并转换为拼音，并将拼音的首字母取出装入set数组
    [self.dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        FriendModel *model = (FriendModel *)obj;
        
        NSString *eName = [self obtainEnglishName:model.name];
        model.englisgName = eName;
        
        NSString *firstWord = [eName substringToIndex:1];
        
        [enSet addObject:firstWord];
        [modelArr addObject:model];

    }];
    
    self.friendDict = [NSMutableDictionary dictionary];
    

    [enSet enumerateObjectsUsingBlock:^(id  _Nonnull obj, BOOL * _Nonnull stop) {
        
        NSMutableArray *modelArray = [NSMutableArray array];
        
        NSString *firstWord = (NSString *)obj;
        
        
        [modelArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            FriendModel *friendsModel = (FriendModel *)obj;
            
            if([friendsModel.englisgName hasPrefix:firstWord]) {
                
                [modelArray addObject:friendsModel];
            }
            
        }];
        
        [self.friendDict setValue:modelArray forKey:firstWord];
        
    }];
    
    
    NSArray *allKeys = [self.friendDict allKeys];
    
    self.resultArray = [allKeys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        
        return [obj1 compare:obj2 options:NSNumericSearch];
    }];
    
    
}


- (NSString *)obtainEnglishName:(NSString*)name
{
    

        
    //转成了可变字符串
    NSMutableString *strC = [NSMutableString stringWithString:name];
    //先转换为带声调的拼音
    CFStringTransform((CFMutableStringRef)strC,NULL, kCFStringTransformMandarinLatin,NO);
    //再转换为不带声调的拼音
    CFStringTransform((CFMutableStringRef)strC,NULL, kCFStringTransformStripDiacritics,NO);
    //转化为大写拼音
    NSString *pinYin = [strC capitalizedString];

    return pinYin;

}



#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.resultArray.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    NSInteger count = [[self.friendDict valueForKey:self.resultArray[section]] count];
    
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellID = @"FriendCell";
    
    FriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell == nil) {
        
        cell = [[FriendTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    FriendModel *model = [self.friendDict valueForKey:self.resultArray[indexPath.section]][indexPath.row];

    cell.nameLabel.text = model.name;
    
    cell.ageLabel.text = [NSString stringWithFormat:@"%d",model.age];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 88;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 22;

}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

//    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(20, 0, self.view.frame.size.width - 20, 22)];
    UILabel *titleLab= [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 60, 22)];
    titleLab.textColor = [UIColor blueColor];
    titleLab.backgroundColor = [UIColor lightGrayColor];
    titleLab.text = self.resultArray[section];
//    [headerView addSubview:titleLab];
    return titleLab;
}


@end
